package com.example.api.alipay;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = "/api/alipay")
public class AlipayAction {

    private final Logger log = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;
    
    @Autowired
    private RestTemplate restTemplate;
    
//    @Autowired
//    private AlipayConfig test;

    @PreAuthorize("#oauth2.hasScope('read')")
    @RequestMapping(value = "/create-trade")
    public String add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        return "From Service-A, "+instance.getServiceId()+" Result is " + r;
    }

    /*//A服务调用B服务
    @RequestMapping(value="testServiceB",method=RequestMethod.GET)
    public String testServiceB(@RequestParam Integer a,@RequestParam Integer b){
    	return restTemplate.getForObject("http://localhost:7075/add?a="+a+"&b="+b, String.class);
    }*/
    
}